# 分页类

from werkzeug.utils import send_file
from test_queue import MysqlDb


# 实例化分页类
class Page:

    #                   总页数   当前页  每页数量
    def __init__(self, total=0, page=1, num=5):

        self.total = int(total)
        self.page = int(page)
        self.num = int(num)

        if self.page <= 0:
            self.page = 1

        if self.num <= 0:
            self.num = 1

    # 计算页数
    def page_num(self):

        # 总数 / 每页显示个数
        if self.total % self.num == 0:

            return self.total // self.num

        else:

            return self.total // self.num + 1

    # 获取分页后的数据
    def get_data(self, sql):

        # limit ,offset     (page-1) * num , num
        #  _变量名  私有属性,只能在此def中调用

        _limit = (self.page - 1) * self.num

        _offset = self.num

        # print(sql)

        sql += " limit %s,%s" % (_limit, _offset)
        # 打印sql语句
        # print(sql)
        # 执行sql查询
        db = MysqlDb()

        res = db.read_list(sql)

        return res

# page = Page()
# print(page.get_data('select * from `order_1` where uid = 1 union select * from `order_2` where uid = 1 union select * from `order_3` where uid = 1 order by date desc'))
